package com.cn.wanxi.util;

import com.cn.wanxi.model.UserModel;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 手写jdbc的思路
 * 1.加载驱动
 * 2.取得连接
 * 3.编译sql
 * 4.执行sql
 * 5.关闭连接
 */
public class JDBC {
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //    对于用户来说，每次操作都要连接数据库，都要取得连接，
//    但是每次都应该只有一个人操作数据库，唯一
//    单例模式：

    /**
     * 得到连接
     */
    private static Connection getConnection() {
        Connection connection = null;
        String url = "jdbc:mysql://121.199.55.96:3306/canyin04";
        String user = "canyin04";
        String password = "123456";
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //    查询(查询所有，根据条件查询)  更新（新增，修改，删除）

    public static ResultSet query(String sql, List<Object> list) {
//        PreparedStatement 和Statement的区别？
        try {
            PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
//            preparedStatement.setObject(1, "");
//            preparedStatement.setObject(2, "");
            for (int i = 0; i < list.size(); i++) {
                preparedStatement.setObject(i + 1, list.get(i));
            }
            return preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int update(String sql, List<Object> list) {
//        PreparedStatement 和Statement的区别？
        try {
            PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
            for (int i = 0; i < list.size(); i++) {
                preparedStatement.setObject(i + 1, list.get(i));
            }
            return preparedStatement.executeUpdate();//影響的数据条数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //    public static void main(String[] args) {
//        ResultSet query = query("select * from user", null);
//        List<UserModel> list = new ArrayList<>();
//        try {
//            while (query.next()) {
//                UserModel model = new UserModel();
//                model.setId(query.getInt("id"));
//                model.setCreateTime(query.getString("create_time"));
//                list.add(model);
//            }
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        System.out.println(list);
//    }
    public static String getMD5Str(String str) {
        byte[] digest = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            digest = md5.digest(str.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //16是表示转换为16进制数
        String md5Str = new BigInteger(1, digest).toString(16);
        return md5Str;
    }
}
